package com.item.model.config;

import com.item.model.interceptor.HandlerInterceptors;
import com.item.model.interceptor.RequestLogInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author zhang-rongyao
 * @version V1.0
 * @Package com.item.model.config
 * @date 2021/7/22 15:17
 */

@Configuration
public class WebMvcConfig{

    @Autowired
    HandlerInterceptors handlerInterceptors;

    @Autowired
    RequestLogInterceptor requestLogInterceptor;


    @Bean
    public WebMvcConfigurer webMvcConfigurer(){
        return new WebMvcConfigurer(){

            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                registry.addInterceptor(handlerInterceptors)
                        .addPathPatterns("/**")
                        .excludePathPatterns("/log/aa","/order/pay");
                //自定义注解拦截
                registry.addInterceptor(requestLogInterceptor)
                        .addPathPatterns("/order/pay","/log/aa");
            }

            /**
             * 允许跨域
             */
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedOriginPatterns("*")
                        .allowedMethods("GET", "POST", "PUT", "OPTIONS", "DELETE", "PATCH")
                        .allowCredentials(true).maxAge(3600);
            }

            //资源映射
            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {

                /**
                 * http://localhost:8088/index.html  ==> /templates/footerbar.html
                 * http://localhost:8088/css/starter.css  ==> /static/css/starter.css
                 * 允许访问静态资源
                 */
                registry
                        //请求拦截路径
                        .addResourceHandler("/**")
                        //真实地址  注意不带**
                        .addResourceLocations("classpath:/static/")
                        .addResourceLocations("classpath:/templates/");
            }
        };
    }



}
